/*
 * Jp_ListeClientsSociete.java
 *
 * Created on 6 avril 2008, 16:49
 */

package gui;

import IBO.IClientSociete;
import IUSECASE.IUC_Factory;
import boerrors.CodePostalMinimumMultiplicityException;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  olivierm
 */
public class Jp_ListeClientsSociete extends javax.swing.JPanel {
    
    private DefaultTableModel _tableModele = new DefaultTableModel() {
        public boolean isCellEditable(int rowIndex, int mColIndex) {
            return false;
        }
    };
    private Frm_Application _parent = null;
    private Jp_DetailsClientS _defaultClientS = null;
    private String[] _colName = {"Id","Société","Forme juridique","TVA","Contact: Nom","Contact: Prénom", "Conctact: Email","Conctact: Téléphone"};
    private Vector<IClientSociete> _cltS = null;    
 
    public void initComponents2(Frm_Application parent, Jp_DetailsClientS jpCltDetailsS){
        this._parent = parent;
        this._defaultClientS = jpCltDetailsS;
        this._tableModele.setColumnIdentifiers(this._colName);
    }    
    
    
    public void refreshClientsList(){
        try {

            String[] objTmp = new String[8];
            this._tableModele.setRowCount(0);
            IUC_Factory factory = this._parent.getFactoryRmi();
            this._cltS = factory.getGestionClients().listerClientsSociete();
            for (int i = 0; i < this._cltS.size(); i++){
                objTmp[0] = String.valueOf(this._cltS.get(i).getIdClient());
                objTmp[1] = this._cltS.get(i).getNomSociete();
                objTmp[2] = "";//this._cltS.get(i).getNomSociete();
                objTmp[3] = this._cltS.get(i).getNumeroTva();
                objTmp[4] = this._cltS.get(i).getNom();
                objTmp[5] = this._cltS.get(i).getPrenom();
                objTmp[6] = this._cltS.get(i).getEmail();
                objTmp[7] = this._cltS.get(i).getTelephone();
                this._tableModele.addRow(objTmp);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Jp_ListeClientsSociete.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    
    /** Creates new form Jp_ListeClientsSociete */
    public Jp_ListeClientsSociete() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jbtn_Details = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_ClientP = new javax.swing.JTable();
        jXStatusBar1 = new org.jdesktop.swingx.JXStatusBar();
        jlbl_NbrElements__ = new javax.swing.JLabel();
        jlbl_NbrElements = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jbtn_Fermer = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel7.setBackground(new java.awt.Color(0, 102, 153));
        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel11.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("Liste des clients");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addContainerGap(395, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addContainerGap())
        );

        add(jPanel7, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jbtn_Details.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/house.png"))); // NOI18N
        jbtn_Details.setText("Détails");
        jbtn_Details.setFocusable(false);
        jbtn_Details.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtn_Details.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtn_Details.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_DetailsActionPerformed(evt);
            }
        });
        jToolBar1.add(jbtn_Details);
        jToolBar1.add(jSeparator1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/database_refresh.png"))); // NOI18N
        jButton2.setText("Rafraichir");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jPanel2.add(jToolBar1, java.awt.BorderLayout.NORTH);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new java.awt.BorderLayout());

        jTable_ClientP.setModel(this._tableModele);
        jScrollPane1.setViewportView(jTable_ClientP);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jlbl_NbrElements__.setText("Nombre d'éléments: ");
        jXStatusBar1.add(jlbl_NbrElements__);
        jXStatusBar1.add(jlbl_NbrElements);

        jPanel1.add(jXStatusBar1, java.awt.BorderLayout.SOUTH);

        jPanel2.add(jPanel1, java.awt.BorderLayout.CENTER);

        add(jPanel2, java.awt.BorderLayout.CENTER);

        jbtn_Fermer.setText("Fermer");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(439, Short.MAX_VALUE)
                .addComponent(jbtn_Fermer)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtn_Fermer)
                .addGap(32, 32, 32))
        );

        add(jPanel3, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//        try {
//
//            String[] objTmp = new String[8];
//            this._tableModele.setRowCount(0);
//            IUC_Factory factory = this._parent.getFactoryRmi();
//            this._cltS = factory.getGestionClients().listerClientsSociete();
//            for (int i = 0; i < this._cltS.size(); i++){
//                objTmp[0] = String.valueOf(this._cltS.get(i).getIdClient());
//                objTmp[1] = this._cltS.get(i).getNomSociete();
//                objTmp[2] = "";//this._cltS.get(i).getNomSociete();
//                objTmp[3] = this._cltS.get(i).getNumeroTva();
//                objTmp[4] = this._cltS.get(i).getNom();
//                objTmp[5] = this._cltS.get(i).getPrenom();
//                objTmp[6] = this._cltS.get(i).getEmail();
//                objTmp[7] = this._cltS.get(i).getTelephone();
//                this._tableModele.addRow(objTmp);
//            }
//        } catch (RemoteException ex) {
//            Logger.getLogger(Jp_ListeClientsSociete.class.getName()).log(Level.SEVERE, null, ex);
//        }
        refreshClientsList();
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jbtn_DetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_DetailsActionPerformed
        try {

            IClientSociete clients = this._cltS.elementAt(this.jTable_ClientP.getSelectedRow());
            this._defaultClientS.setClient(clients);
            this._defaultClientS.initComponents2(this._parent);
            this._defaultClientS.refreshInterventions();
            this._parent.frmDetailsS();
        } catch (CodePostalMinimumMultiplicityException ex) {
            Logger.getLogger(Jp_ListeClientsSociete.class.getName()).log(Level.SEVERE, null, ex);
        }  catch (NullPointerException ex){
            JOptionPane.showMessageDialog(this,"Veuillez sélectionner un client.","Information",JOptionPane.INFORMATION_MESSAGE);
        } catch (ArrayIndexOutOfBoundsException ex){
            JOptionPane.showMessageDialog(this,"Veuillez sélectionner un client.","Information",JOptionPane.INFORMATION_MESSAGE);
        }
        
    }//GEN-LAST:event_jbtn_DetailsActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTable jTable_ClientP;
    private javax.swing.JToolBar jToolBar1;
    private org.jdesktop.swingx.JXStatusBar jXStatusBar1;
    private javax.swing.JButton jbtn_Details;
    private javax.swing.JButton jbtn_Fermer;
    private javax.swing.JLabel jlbl_NbrElements;
    private javax.swing.JLabel jlbl_NbrElements__;
    // End of variables declaration//GEN-END:variables
    
}
